package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用有参构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Person p = new Person("刘苍松", 55);
        System.out.println(p);

        //加载类对象
        Class cls = Class.forName("reflect.Person");
        //获取无参构造器
//        Constructor c = cls.getConstructor();
//        Object o  = c.newInstance();

        //先获取指定的构造器：Person(String name,int age)
        Constructor c = cls.getConstructor(String.class, int.class);
        Object o = c.newInstance("刘苍松", 55);
        System.out.println(o);
    }
}
